#include <windows.h>
#include <iostream>
using namespace std;

int tickets = 1001;
HANDLE g_hEvent;

DWORD WINAPI Fun1Proc(LPVOID lpParameter)
{
	while(TRUE)
	{
		WaitForSingleObject(g_hEvent, INFINITE);
		if(tickets > 0)
		{
			Sleep(1);
			cout<<"thread1 sell tickets : "<<tickets--<<endl;
			SetEvent(g_hEvent);
		}
		else
		{
			SetEvent(g_hEvent);
			break;
		}
	}
	return 0;
}

DWORD WINAPI Fun2Proc(LPVOID lpParameter)
{
	while(TRUE)
	{
		WaitForSingleObject(g_hEvent, INFINITE);
		if(tickets > 0)
		{
			Sleep(1);
			cout<<"thread2 sell tickets : "<<tickets--<<endl;
			SetEvent(g_hEvent);
		}
		else
		{
			SetEvent(g_hEvent);
			break;
		}
	}
	return 0;
}
int main()
{
	HANDLE hTread1;
	HANDLE hTread2;

	g_hEvent = CreateEventW(NULL, FALSE, FALSE, L"tickets");
	
	SetEvent(g_hEvent);

	hTread1 = CreateThread(NULL, 0, Fun1Proc, NULL, 0, NULL);
	hTread2 = CreateThread(NULL, 0, Fun2Proc, NULL, 0, NULL);
	CloseHandle(hTread1);
	CloseHandle(hTread2);

	Sleep(10001);
	CloseHandle(g_hEvent);
	return 0;
}